from elasticsearch_dsl import Document, Integer, Text, Date
from elasticsearch_dsl.query import MultiMatch


class SearchWord(Document):
    id = Integer()
    word = Text(analyzer='ik_max_word')
    save_time = Date()
    ip = Text(analyzer='ik_max_word')

    class Index:
        name = "search_word"


def get_recommend_words(one_word):
    if one_word == "":
        return []
    mul_math = MultiMatch(query=one_word, fields=['word'])
    res = SearchWord.search().query(mul_math)

    words_list = []
    for one_res in res:
        if one_res.word != "":
            words_list.append(one_res.word)
    words_list = list(set(words_list))
    return words_list
